// pages/index/collection/collection.js
import Toast from "../../../miniprogram_npm/vant-weapp/toast/toast";
import Dialog from '../../../miniprogram_npm/vant-weapp/dialog/dialog';

const app = getApp()
//获取 log 日志打印
const log = new app.appLogs()
//获取 http 网络请求
// const http = new app.appHttp()
const http = app.http
Page({

    /**
     * 页面的初始数据
     */
    data: {
        djid: '',
        is_sex: '',
        Type: [],
        image: "",
        showdj: false,
        stationdj: "",
        columnsdj: [],
        showsex: false,
        stationsex: "",
        valueeducation: "",
        valueposition: "",
        columnssex: [{
            id: null,
            text: "请选择性别"
        }, {
            id: 1,
            text: "男"
        }, {
            id: 2,
            text: "女"
        }],
        stationbirthday: "",
        stationData: "",
        fileList: [],
        columnsnation: ["下拉选择民族", "汉族", "蒙古族", "回族", "藏族", "维吾尔族", "苗族", "彝族", "壮族", "布依族", "朝鲜族", "满族", "侗族", "瑶族", "白族", "土家族",
            "哈尼族", "哈萨克族", "傣族", "黎族", "傈僳族", "佤族", "畲族", "高山族", "拉祜族", "水族", "东乡族", "纳西族", "景颇族", "柯尔克孜族",
            "土族", "达斡尔族", "仫佬族", "羌族", "布朗族", "撒拉族", "毛南族", "仡佬族", "锡伯族", "阿昌族", "普米族", "塔吉克族", "怒族", "乌孜别克族",
            "俄罗斯族", "鄂温克族", "德昂族", "保安族", "裕固族", "京族", "塔塔尔族", "独龙族", "鄂伦春族", "赫哲族", "门巴族", "珞巴族", "基诺族"],
        shownation: false,
        stationnation: "",

        minHour: 10,
        maxHour: 20,
        currentDate: new Date().getTime(),
        minDate: new Date(1900, 1, 1).getTime(),
        maxDate: new Date().getTime(),
        minDate1: new Date(1900, 1, 1).getTime(),
        maxDate1: new Date().getTime(),
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        const _this = this
        this.setData({
            theme: app.globalData.theme,
            time: _this.getTime()

        })
        wx.setNavigationBarColor({
            frontColor: '#ffffff',
            backgroundColor: app.globalData.theme.themeColor[0]
        })
        this.getTime()
        this.dj()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    },
    //党建
    onConfirmdj(event) {
        const than = this
        console.log(JSON.stringify(event))
        var text = event.detail.value.text;//文字
        var id = event.detail.value.id;//id
        var warehouseContent = ""; //选择框显示的文字
        warehouseContent = text;//文字显示
        than.setData({
            showdj: false,
            stationdj: warehouseContent,
            djid: id
        });
    },
    onCanceldj() {
        this.setData({
            showdj: false
        })
        log.l('取消');
    },
    onChangedj(event) {
        const { picker, value, index } = event.detail;
    },
    showPopupdj() {
        this.setData({
            showdj: true
        });
    },
    onClosedj() {
        this.setData({
            showdj: false
        });
    },
    //性别
    onConfirmsex(event) {
        const than = this
        console.log(JSON.stringify(event))
        var text = event.detail.value.text;//文字
        var id = event.detail.value.id;//id
        var warehouseContent = ""; //选择框显示的文字
        warehouseContent = text;//文字显示
        this.setData({
            showsex: false,
            stationsex: warehouseContent,
        });
        log.l(text + "aaa")
        than.setData({
            is_sex: id
        });
    },
    onCancelsex() {
        this.setData({
            showsex: false
        })
        log.l('取消');
    },
    onChangesex(event) {
        const { picker, value, index } = event.detail;
    },
    showPopupsex() {
        this.setData({
            showsex: true
        });
    },
    onClosesex() {
        this.setData({
            showsex: false
        });
    },

    //出生日期
    onConfirmbirthday(event) {
        const _this = this
        log.e(event.detail)
        this.setData({
            showbirthday: false,
            currentDate: event.detail,
            time: _this.getTime(event.detail),
            stationbirthday: _this.getTime(event.detail)
        });
        log.e(this.data.time)
        console.log(event.detail);
    },
    showPopupbirthday() {
        this.setData({
            showbirthday: true
        })
    },
    onClosebirthday() {
        this.setData({
            showbirthday: false
        });
    }
    ,
    onCancelbirthday() {
        this.setData({
            showbirthday: false
        })
        log.l('取消');
    },
    /**
     * 获取当前时间
     */
    getTime: function (timestamp) {
        if (timestamp == null) {
            timestamp = Date.parse(new Date())
        }
        timestamp = timestamp / 1000
        //获取当前时间
        let n = timestamp * 1000;
        let date = new Date(n);
        //年
        let Y = date.getFullYear();
        //月
        let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
        //日
        let D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
        this.setData({
            timestamp: n,
            tempTime: {
                Y: Y,
                M: M,
                D: D,
            }
        })
        const time = Y + "-" + M + "-" + D
        log.l(time)
        return time
    }
    ,
    //民族
    onConfirmnation(event) {
        log.e(event.detail)
        this.setData({
            shownation: false,
            stationnation: event.detail.value
        });

    },
    showPopupnation() {
        this.setData({
            shownation: true
        })
    },
    onClosenation() {
        this.setData({
            shownation: false
        });
    }
    ,
    onCancelnation() {
        this.setData({
            shownation: false
        })
        log.l('取消');
    },
    onChangenation(event) {
        const { picker, value, index } = event.detail;
    },
    dj: function () {
        let Type = [];
        const than = this
        http.get("dj/getOrgList").data({
            area_code: "86.51.01.85.01.31"
        }).send().success(res => {
            log.e(JSON.stringify(res))
            res.data.forEach((item, index) => {
                Type.push({
                    text: item.name,
                    id: item.id
                });
            });
            than.setData({
                Type: Type,
                columnsdj: Type
            })
        }).fail(res => {

        })
    },
    /**
     * 图片上传
     */
    afterRead: function (e) {
        log.l("图片 : " + JSON.stringify(e.detail))
        const _this = this
        for (let i = 0; i < e.detail.file.length; i++) {
            _this.data.fileList.push({
                url: e.detail.file[i].path,
                name: e.detail.file[i].size
            })
        }
        log.l("图片数组 : " + JSON.stringify(_this.data.fileList))
        this.setData({
            fileList: _this.data.fileList
        })
        this.uploadcardFile();
    },
    /**
     * 删除图片
     */
    deleteImage1: function (e) {
        log.l("图片 : " + JSON.stringify(e.detail))
        const _this = this
        const index = e.detail.index
        const deleteData = this.data.fileList.splice(index, 1)
        log.l("删除图片: " + JSON.stringify(deleteData))
        this.setData({
            fileList: _this.data.fileList
        })
    },
    uploadcardFile: function () {
        var than = this
        log.l("开始上传1 : " + than.data.fileList[0].url)
        wx.uploadFile({
            url: "https://www.xinwangzn.com/api/mine/uploading",
            filePath: than.data.fileList[0].url,
            name: "file",
            formData: {
                savePath: "/applets/photo",
            },
            success(res) {
                log.l("上传图片成功1: " + JSON.stringify(res))
                var cityjson = JSON.parse(res.data);
                than.setData({
                    image: cityjson.data[0]
                })
                log.e("执行了" + than.data.image)
            },
            fail(res) {
                log.l("上传图片失败1: " + JSON.stringify(res))
            }
        })
    },
    /*学历*/
    onChangephoneeducation(event) {
        console.log(event.detail);
        this.setData({
            education: event.detail
        })
        log.e(this.data.education + "学历")
    }, /*职位*/
    onChangephoneposition(event) {
        console.log(event.detail);
        this.setData({
            position: event.detail
        })
        log.e(this.data.position + "职位")
    },
    onChangename(event) {
        this.setData({
            name: event.detail
        })
    },
    //入党日期
    onConfirmdate(event) {
        const _this = this
        log.e(event.detail)
        this.setData({
            showdate: false,
            currentDate1: event.detail,
            time1: _this.getTime(event.detail),
            stationdate1: _this.getTime(event.detail)
        });
        log.e(this.data.time)
        console.log(event.detail);
    },
    showPopupdate() {
        this.setData({
            showdate: true
        })
    },
    onClosedate() {
        this.setData({
            showdate: false
        });
    }
    ,
    onCanceldate() {
        this.setData({
            showdate: false
        })
        log.l('取消');
    },
    onClisk() {
        const than = this
        log.e("照片：" + than.data.image)
        log.e("性别" + than.data.is_sex)
        log.e("党组织id" + than.data.djid)
        log.e("入党" + than.data.time1)
        log.e("出生" + than.data.time)
        log.e("职位" + than.data.position)
        log.e("学历" + than.data.education)
        log.e("姓名" + than.data.name)
        log.e("民族" + than.data.stationnation)
        if (than.data.image != '') {
            if (than.data.name != undefined) {
                if (than.data.is_sex != '') {
                    if (than.data.djid != '') {
                        if (than.data.time1 != undefined) {
                            if (than.data.time != '') {

                                if (than.data.education != undefined) {
                                    if (than.data.stationnation != '') {
                                        http.get("dj/addPartyOrgUserAudit").data({
                                            party_org_id: than.data.djid,
                                            head_img: than.data.image,
                                            name: than.data.name,
                                            sex: than.data.is_sex,
                                            birthday: than.data.time,
                                            nation: than.data.stationnation,
                                            education: than.data.education,
                                            party_date: than.data.time1,
                                            position: than.data.position
                                        }).send().success(res => {
                                            Toast(res.data.msg);
                                            wx.navigateBack({
                                                delta: -1
                                            });
                                            log.e(res.data.msg)
                                        }).fail(res => {
                                            Toast("保存失败");
                                            wx.navigateBack({
                                                delta: -1
                                            });
                                            log.e(res.data.msg)
                                        })
                                    } else {
                                        Dialog.alert({
                                            message: '请选择民族',
                                        }).then(() => {
                                        });
                                    }
                                } else {
                                    Dialog.alert({
                                        message: '请输入学历',
                                    }).then(() => {
                                    });
                                }

                            } else {
                                Dialog.alert({
                                    message: '请选择出生日期',
                                }).then(() => {
                                });
                            }
                        } else {
                            Dialog.alert({
                                message: '请选择入党时间',
                            }).then(() => {
                            });
                        }
                    } else {
                        Dialog.alert({
                            message: '请选择党组织',
                        }).then(() => {
                        });
                    }
                } else {
                    Dialog.alert({
                        message: '请选择性别',
                    }).then(() => {
                    });
                }
            } else {
                Dialog.alert({
                    message: '请输入姓名',
                }).then(() => {
                });
            }
        } else {
            Dialog.alert({
                message: '请选择图片',
            }).then(() => {
            });
        }
    }
})